package com.masterclient.app.businessmodule.vipmodule.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.masterclient.app.R;
import com.masterclient.app.businessmodule.vipmodule.activity.SaleOrderDetailActivity;
import com.masterclient.app.businessmodule.vipmodule.bean.Order;
import com.masterclient.app.businessmodule.vipmodule.card.IBaseCard;
import com.masterclient.app.businessmodule.vipmodule.card.OrderDfhCard;
import com.masterclient.app.businessmodule.vipmodule.card.OrderDfkCard;
import com.masterclient.app.businessmodule.vipmodule.card.OrderDshCard;
import com.masterclient.app.businessmodule.vipmodule.card.OrderYwcCard;
import com.masterclient.app.businessmodule.vipmodule.card.OrderYyjCard;
import com.masterclient.app.businessmodule.vipmodule.card.UnKnowCard;

import java.util.ArrayList;
import java.util.List;

/**
 * 零售订单adapter
 */
public class SaleOrderAdapter extends RecyclerView.Adapter<IBaseCard> {

    private Context mContext;
    private List<Order> orderList;

    public SaleOrderAdapter(Context ctx) {
        this.mContext = ctx;
        orderList = new ArrayList<>();
    }

    public void setOrderList(List<Order> orderList) {
        if(null != orderList){
            int start = this.orderList.size();
            this.orderList.addAll(orderList);
            notifyItemRangeChanged(start,orderList.size());
        }

    }

    public void setNewData(List<Order> orderList) {
        this.orderList = orderList;
        notifyDataSetChanged();
    }

    @Override
    public IBaseCard onCreateViewHolder(ViewGroup parent, int viewType) {
        if (0 == viewType || 3 == viewType) {// 待付款 已关闭
            return new OrderDfkCard(LayoutInflater.from(mContext).inflate(R.layout.card_dfk_order, parent, false), mContext);
        } else if (1 == viewType) {// 待发货
            return new OrderDfhCard(LayoutInflater.from(mContext).inflate(R.layout.card_dfh_order, parent, false), mContext);
        } else if (2 == viewType||4 == viewType) {// 代收货订单 已收货
            return new OrderDshCard(LayoutInflater.from(mContext).inflate(R.layout.card_dsh_order, parent, false), mContext);
        } else if (8 == viewType) {// 已完成订单
            return new OrderYwcCard(LayoutInflater.from(mContext).inflate(R.layout.card_ywc_order, parent, false), mContext);
        } else if (9 == viewType) {// 已移交订单
            return new OrderYyjCard(LayoutInflater.from(mContext).inflate(R.layout.card_yyj_order, parent, false), mContext);
        } else {
            return new UnKnowCard(LayoutInflater.from(mContext).inflate(R.layout.card_unknow, parent, false), mContext);
        }

    }


    @Override
    public void onBindViewHolder(final IBaseCard holder, final int position) {
        holder.onBindViewHolder(orderList.get(position), this, position);
        int status = orderList.get(position).getOrder().getStatus();
        String order_no = orderList.get(position).getOrder().getOrder_no();
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, SaleOrderDetailActivity.class);
            intent.putExtra("status", status);
            intent.putExtra("order_no", order_no);
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemViewType(int position) {
        return orderList.get(position).getOrder().getStatus();
    }

    @Override
    public int getItemCount() {
        return null == orderList ? 0 : orderList.size();
    }

}
